package com.jxd.ordering.controller;

import com.jxd.ordering.annotation.CheckPermission;
import com.jxd.ordering.exception.CommonEnum;
import com.jxd.ordering.exception.serviceexception.PermissionNotAllowedException;
import com.jxd.ordering.model.Order;
import com.jxd.ordering.model.ResultBody;
import com.jxd.ordering.service.IEmpService;
import com.jxd.ordering.service.IOrderService;
import com.jxd.ordering.service.IUserService;
import com.jxd.ordering.util.PermissionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @ClassName OrderController
 * @Description TODO
 * @Author Ankailong
 * @Date 2022/9/30
 * @Version 1.0
 */
@RestController
public class OrderController {
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IEmpService empService;
    @Autowired
    private IUserService userService;

    /**
     * 新增订餐信息
     * level: 1
     * post
     * @param order 订餐信息实体类
     * @return 是否新增成功
     */
    @RequestMapping("/addOrder")
    public ResultBody addOrder(@RequestBody Order order) {
        return orderService.addOrder(order) ? ResultBody.success() : ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
    }

    /**
     * 修改订餐信息
     * level: 1
     * post
     * @param order 订餐信息实体类
     * @return 是否修改成功
     */
    @RequestMapping("/editOrder")
    public ResultBody editOrder(@RequestBody Order order) {
        return orderService.editOrder(order) ? ResultBody.success() : ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
    }

    /**
     * 修改订单状态
     * level: 2
     * post
     * @param map 包含 orderno 订单编号 statu 订单状态
     * @return 是否修改成功
     */
    @CheckPermission("2")
    @RequestMapping("/editOrderStatu")
    public ResultBody editOrderStatu(@RequestBody Map<String, String> map) {
        int orderno = Integer.parseInt(map.get("orderno").toString());
        int statu = Integer.parseInt(map.get("statu").toString());
        return orderService.editOrderStatu(orderno, statu) ? ResultBody.success() : ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
    }

    /**
     * 批量删除订单
     * level: 3
     * post
     * @param ordernos 订单编号数组
     * @return 是否删除成功
     */
    @CheckPermission("3")
    @RequestMapping("/delOrder")
    public ResultBody delOrder(@RequestBody List<Integer> ordernos) {
        return orderService.delOrder(ordernos.stream().mapToInt(Integer::intValue).toArray()) ? ResultBody.success() : ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
    }

    /**
     * 分页查询订单信息
     * level: 3
     * post
     * @param map 包含 int page, int limit, String start, String end, empno, int type, int statu
     * @return 查询结果
     */
    @RequestMapping("/getOrderPyPage")
    public Map<String, Object> getOrderPyPage(@RequestBody Map<String, String> map, HttpServletRequest request) throws PermissionNotAllowedException {
        if (map.containsKey("userid")) {
            int userid = Integer.parseInt(map.get("userid").toString());

            //临时授权
            if (userid != Integer.parseInt(request.getAttribute("userid").toString())) {
                PermissionUtil.PermissionControl(request.getAttribute("permission"), 3);
            }
        }
        return orderService.getOrderPyPage(map);
    }

    /**
     * 按订单编号查询订单信息
     * level: 3
     * @param orderno 订单编号
     * @return 查询结果
     */
    @RequestMapping("getOrderByOrderno")
    public Order getOrderByOrderno(int orderno, HttpServletRequest request) throws PermissionNotAllowedException {
        int empno = orderService.getOrderByOrderno(orderno).getEmpno();
        int deptno = userService.getDeptnoByUserid(Integer.parseInt(request.getAttribute("userid").toString()));
        int orderDeptno = empService.getEmpByEmpno(orderService.getOrderByOrderno(orderno).getEmpno()).getDeptno();

        //临时授权
        if ((Integer.parseInt(request.getAttribute("permission").toString()) == 2 && deptno != orderDeptno) ||
                (Integer.parseInt(request.getAttribute("permission").toString()) != 2 && empno != userService.getUsernoByUserid(Integer.parseInt(request.getAttribute("userid").toString())))) {
            PermissionUtil.PermissionControl(request.getAttribute("permission"), 3);
        }
        return orderService.getOrderByOrderno(orderno);
    }

    /**
     * 修改订餐人员信息
     * level: 3
     * post
     * @param map 包含 orderno 订单号 empnos 订餐人员编号数组
     * @return 是否修改成功
     */
    @RequestMapping("/editOrderEmp")
    public ResultBody editOrderEmp(@RequestBody Map<String, Object> map, HttpServletRequest request) throws PermissionNotAllowedException {
        int orderno = Integer.parseInt(map.get("orderno").toString());
        int[] empnos = ((List<Integer>)map.get("empnos")).stream().mapToInt(Integer::intValue).toArray();
        int empno = orderService.getOrderByOrderno(orderno).getEmpno();
        //临时授权
        if (empno != userService.getDeptnoByUserid(Integer.parseInt(request.getAttribute("userid").toString()))) {
            PermissionUtil.PermissionControl(request.getAttribute("permission"), 3);
        }
        return orderService.editOrderEmp(orderno, empnos)? ResultBody.success() : ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
    }

    /**
     * 按订单号查询所有订餐人员信息
     * level: 3
     * @param orderno 订单编号
     * @return 查询结果
     */
    @RequestMapping("/getAllOrderEmp")
    public List<Map<String, String>> getAllOrderEmp(int orderno, HttpServletRequest request) throws PermissionNotAllowedException {
        int empno = orderService.getOrderByOrderno(orderno).getEmpno();
        int deptno = userService.getDeptnoByUserid(Integer.parseInt(request.getAttribute("userid").toString()));
        int orderDeptno = empService.getEmpByEmpno(orderService.getOrderByOrderno(orderno).getEmpno()).getDeptno();

        //临时授权
        if ((Integer.parseInt(request.getAttribute("permission").toString()) == 2 && deptno != orderDeptno) ||
                (Integer.parseInt(request.getAttribute("permission").toString()) != 2 && empno != userService.getUsernoByUserid(Integer.parseInt(request.getAttribute("userid").toString())))) {
            PermissionUtil.PermissionControl(request.getAttribute("permission"), 3);
        }
        return orderService.getAllOrderEmp(orderno);
    }

    /**
     * 修改订餐餐品信息
     * level: 3
     * post
     * @param map 包含 订单号 餐品信息 包含 mealno 餐品编号 count 数量 remark 备注
     * @return 是否修改成功
     */
    @RequestMapping("/editOrderMeal")
    public ResultBody editOrderMeal(@RequestBody Map<String, Object> map, HttpServletRequest request) throws PermissionNotAllowedException {
        int orderno = Integer.parseInt(map.get("orderno").toString());
        Map<String, Map<String, String>> meals = (Map<String, Map<String, String>>)map.get("meals");
        int empno = orderService.getOrderByOrderno(orderno).getEmpno();
        //临时授权
        if (empno != userService.getDeptnoByUserid(Integer.parseInt(request.getAttribute("userid").toString()))) {
            PermissionUtil.PermissionControl(request.getAttribute("permission"), 3);
        }
        return orderService.editOrderMeal(orderno, meals)? ResultBody.success() : ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
    }

    /**
     * 根据订单号和餐品编号查询订餐餐品信息
     * level: 2
     * get
     * @param orderno 订单编号
     * @param mealno 餐品编号
     * @return 查询结果
     */
    @RequestMapping("/getOrderMealByMealno")
    public Map<String, Object> getOrderMealByMealno(int orderno, int mealno, HttpServletRequest request) throws PermissionNotAllowedException {
        int empno = orderService.getOrderByOrderno(orderno).getEmpno();
        //临时授权
        if (empno != userService.getUsernoByUserid(Integer.parseInt(request.getAttribute("userid").toString()))) {
            PermissionUtil.PermissionControl(request.getAttribute("permission"), 2);
        }
        return orderService.getOrderMealByMealno(orderno, mealno);
    }

    /**
     * 按订单号查询所有订餐餐品信息
     * level: 3
     * get
     * @param orderno 订单编号
     * @return 查询结果
     */
    @RequestMapping("/getAllOrderMeal")
    public Map<String, Map<String, Object>> getAllOrderMeal(int orderno, HttpServletRequest request) throws PermissionNotAllowedException {
        int empno = orderService.getOrderByOrderno(orderno).getEmpno();
        int deptno = userService.getDeptnoByUserid(Integer.parseInt(request.getAttribute("userid").toString()));
        int orderDeptno = empService.getEmpByEmpno(orderService.getOrderByOrderno(orderno).getEmpno()).getDeptno();

        //临时授权
        if ((Integer.parseInt(request.getAttribute("permission").toString()) == 2 && deptno != orderDeptno) ||
                (Integer.parseInt(request.getAttribute("permission").toString()) != 2 && empno != userService.getUsernoByUserid(Integer.parseInt(request.getAttribute("userid").toString())))) {
            PermissionUtil.PermissionControl(request.getAttribute("permission"), 3);
        }
        return orderService.getAllOrderMeal(orderno);
    }
}
